<template>
  <div class="app-container">
    <!-- 主登录区域 -->
    <div class="login-container">
      <div class="login-card">
        <div class="login-header">
          <h2 data-cy="login-title">
            用户登录
          </h2>
          <p>欢迎回来，请登录您的账号</p>
        </div>

        <form
          data-cy="login-form"
          @submit.prevent="handleSubmit"
        >
          <div class="form-group">
            <label for="account">账号</label>
            <div class="input-with-icon">
              <i class="fas fa-user" />
              <input
                id="account"
                v-model="account"
                type="text"
                placeholder="请输入您的账号"
                maxlength="10"
                data-cy="account-input"
              >
            </div>
            <div class="input-hint">
              请输入注册时使用的账号
            </div>
          </div>

          <div class="form-group">
            <label for="password">密码</label>
            <div class="input-with-icon">
              <i class="fas fa-lock" />
              <input
                id="password"
                v-model="password"
                type="password"
                placeholder="请输入您的密码"
                maxlength="20"
                data-cy="password-input"
              >
            </div>
            <div class="input-hint">
              请输入您的登录密码
            </div>
          </div>

          <button
            type="submit"
            class="btn-submit"
            data-cy="login-button"
          >
            登录
          </button>
        </form>

        <div class="register-link">
          没有账号？<router-link
            to="/register"
            data-cy="register-link"
          >
            立即注册
          </router-link>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { userAPI } from '@/api/user'
import { useUserStore } from '@/stores/userStore';
import { useRouter } from 'vue-router'

export default {
  name: 'LoginPanel',
  data() {
    return {
      account: '',
      password: ''
    }
  },
  methods: {
    async handleSubmit() {
      if (!this.account || !this.password) {
        alert('请填写账号和密码');
        return;
      }

      if (this.account.length > 10) {
        alert('账号不能超过10个字符');
        return;
      }

      if (this.password.length > 20) {
        alert('密码不能超过20个字符');
        return;
      }

      try {
        const response = await userAPI.login({
          userAccount: this.account,
          userPassword: this.password
        });

        const apiResponse = response.data;
        if (!apiResponse || apiResponse.code !== 200 || !apiResponse.data) {
          alert('请检查账号或密码是否正确');
          this.account = '';
          this.password = '';
          return;
        }

        const loginData = apiResponse.data;
        const userStore = useUserStore();
        // 传递用户信息给store，包含完整的用户数据
        userStore.login({
          userId: loginData.userInfo.userId,
          userName: loginData.userInfo.userName,
          userAccount: loginData.userInfo.userAccount,
          token: loginData.token
        });

        this.$router.push('/mainPage');
      } catch (error) {
        console.log('登录失败', error);
        // 显示具体的错误信息
        if (error.response) {
          // 服务器返回了错误状态码
          if (error.response.status === 401 || error.response.status === 403) {
            alert('账号或密码错误，请重新输入');
          } else if (error.response.data && error.response.data.message) {
            // 显示服务器返回的错误信息
            alert(`登录失败: ${error.response.data.message}`);
          } else {
            alert(`登录失败: 服务器返回状态码 ${error.response.status}`);
          }
        } else if (error.request) {
          // 请求已发送但没有收到响应
          alert('登录失败: 无法连接到服务器，请检查网络连接');
        } else {
          // 请求设置时出现问题
          alert('登录失败: ' + error.message);
        }
        this.password = ''; // 清空密码字段，保留账号以便用户重新输入
      }
    }
  }
}
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
}

.app-container {
  min-height: 100vh;
  background: #f5f7fa;
}

/* 顶部导航栏样式（与首页完全一致） */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 15px 40px;
  background: white;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.08);
}

.logo {
  display: flex;
  align-items: center;
  font-size: 22px;
  font-weight: 700;
  color: #1a73e8;
}

.logo-icon {
  width: 36px;
  height: 36px;
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 12px;
  color: white;
}

.nav-items {
  display: flex;
}

.nav-item {
  padding: 8px 16px;
  font-size: 16px;
  color: #5f6368;
  cursor: pointer;
  border-radius: 4px;
  margin-right: 10px;
  transition: all 0.2s;
}

.nav-item.active {
  background-color: #e8f0fe;
  color: #1a73e8;
  font-weight: 500;
}

.user-info {
  display: flex;
  align-items: center;
}

.avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 16px;
  margin-right: 10px;
}

.username {
  font-size: 16px;
  font-weight: 500;
  color: #202124;
  margin-right: 15px;
}

.account-btn {
  padding: 8px 16px;
  background-color: #f8f9fa;
  border: 1px solid #dadce0;
  border-radius: 4px;
  color: #3c4043;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
}

.account-btn:hover {
  background-color: #f1f3f4;
  border-color: #d2e3fc;
}

/* 登录区域样式 */
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: calc(100vh - 100px);
  padding: 20px;
}

.login-card {
  background: white;
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.12);
  padding: 40px 50px;
  width: 100%;
  max-width: 500px;
  animation: fadeInUp 0.6s ease-out forwards;
  position: relative;
  overflow: hidden;
}

/* 装饰性元素 */
.login-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 6px;
  background: linear-gradient(90deg, #4facfe 0%, #00f2fe 100%);
}

/* 登录标题样式 */
.login-header {
  text-align: center;
  margin-bottom: 30px;
  position: relative;
}

.login-header h2 {
  font-size: 28px;
  font-weight: 700;
  color: #202124;
  margin-bottom: 10px;
}

.login-header p {
  font-size: 16px;
  color: #5f6368;
  opacity: 0.8;
}

/* 表单样式优化 */
.form-group {
  margin-bottom: 24px;
}

.form-group label {
  display: block;
  font-size: 14px;
  font-weight: 500;
  color: #5f6368;
  margin-bottom: 8px;
}

.input-with-icon {
  position: relative;
}

.input-with-icon i {
  position: absolute;
  left: 15px;
  top: 15px;
  color: #888;
  font-size: 18px;
}

.input-with-icon input {
  width: 100%;
  padding: 14px 14px 14px 48px;
  font-size: 16px;
  border: 1px solid #dadce0;
  border-radius: 8px;
  background-color: #f8f9fa;
  transition: all 0.3s;
  height: 52px;
}

.input-with-icon input:focus {
  outline: none;
  border-color: #4facfe;
  box-shadow: 0 0 0 3px rgba(79, 172, 254, 0.15);
  background-color: #fff;
}

.input-hint {
  font-size: 13px;
  color: #888;
  margin-top: 8px;
  line-height: 1.4;
}

/* 按钮优化 */
.btn-submit {
  display: block;
  width: 100%;
  padding: 16px;
  font-size: 16px;
  font-weight: 600;
  color: white;
  background: linear-gradient(135deg, #1a73e8 0%, #0d47a1 100%);
  border: none;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s;
  margin-top: 20px;
  box-shadow: 0 4px 12px rgba(26, 115, 232, 0.25);
  letter-spacing: 1px;
}

.btn-submit:hover {
  background: linear-gradient(135deg, #1669c1 0%, #0a2c5e 100%);
  box-shadow: 0 6px 15px rgba(26, 115, 232, 0.35);
  transform: translateY(-2px);
}

/* 注册链接 */
.register-link {
  text-align: center;
  margin-top: 25px;
  font-size: 15px;
  color: #666;
  padding-top: 15px;
  border-top: 1px solid #f0f0f0;
}

.register-link a {
  color: #1a73e8;
  text-decoration: none;
  font-weight: 500;
  transition: all 0.2s;
  margin-left: 5px;
}

.register-link a:hover {
  color: #0d47a1;
  text-decoration: underline;
}

/* 动画效果 */
@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 响应式设计 */
@media (max-width: 600px) {
  .header {
    padding: 12px 20px;
  }

  .logo span {
    font-size: 18px;
  }

  .nav-item {
    padding: 6px 12px;
    font-size: 14px;
  }

  .username {
    font-size: 14px;
    margin-right: 10px;
  }

  .account-btn {
    padding: 6px 12px;
    font-size: 13px;
  }

  .login-card {
    padding: 30px 25px;
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.08);
  }

  .login-header h2 {
    font-size: 24px;
  }

  .input-with-icon i {
    font-size: 16px;
    top: 16px;
    left: 12px;
  }

  .input-with-icon input {
    padding: 12px 12px 12px 40px;
    height: 48px;
  }
}
</style>
